package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.common.api.delegate.event.FlowableEventListener;
import org.flowable.engine.common.impl.persistence.StrongUuidGenerator;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;

/**
 * Configuration - 可以覆盖flowable默认的一些重要组件，比如eventListener,expressManager等等
 *
 * @author lilh
 * @date 2019-01-28 15:20
 */
//@Component
//@DependsOn("globalFlowableEventListener")
public class FlowableConfiguration implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {

    //@Autowired
    private List<FlowableEventListener> customEventListeners = new ArrayList<>();

    public FlowableConfiguration(List<FlowableEventListener> customEventListeners) {
        this.customEventListeners  = customEventListeners;
    }

    @Override
    public void configure(SpringProcessEngineConfiguration engineConfiguration) {
        List<FlowableEventListener> eventListeners = engineConfiguration.getEventListeners();
        if (CollectionUtils.isEmpty(eventListeners)) {
            eventListeners = new ArrayList<>();
        }
        eventListeners.addAll(customEventListeners);
        engineConfiguration.setEventListeners(eventListeners);
        //设置id生成器
        engineConfiguration.setIdGenerator(new StrongUuidGenerator(){
            @Override
            public String getNextId() {
                return super.getNextId().replaceAll("-", "");
            }
        });

        //act_hi_*相关表的数据使用异步线程的方式入库
        //engineConfiguration.setAsyncHistoryEnabled(true);
        //engineConfiguration.setAsyncHistoryExecutorActivate(true);
    }

}
